package com.example;

import com.example.message.User;
import lombok.SneakyThrows;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CountDownLatch;

@SpringBootApplication
public class RocketmqProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(RocketmqProducerApplication.class,args);
    }
}

@RestController
class TestController{

    @Autowired
    private RocketMQTemplate template;

    @SneakyThrows
    @GetMapping("/string")
    public void testString(String name){
        var destination = "test-topic:string";
        template.sendAndReceive(destination,name,String.class,1,2);
    }

    @SneakyThrows
    @GetMapping("/object")
    public void testObject(String name){
        User user = new User();
        user.setId("1");
        user.setName(name);
        user.setAge(18);
        var destination = "test-topic:object";
        String result = template.sendAndReceive(destination, MessageBuilder.withPayload(user).setHeader(RocketMQHeaders.KEYS,"test").build(),String.class,3000L,1);
        System.out.println("result:"+result);
    }
}
